package org.xhy.sniffer.plugins.mysql.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.xhy.sniffer.interceptor.enhance.InstanceMethodsAroundInterceptor;
import org.xhy.sniffer.plugins.mysql.config.ConnectInfo;
import org.xhy.sniffer.trace.AbstractSpan;
import org.xhy.sniffer.trace.Comment;
import org.xhy.sniffer.trace.ContextManager;

import java.lang.reflect.Method;
import java.util.Arrays;


@Slf4j
public class MySQLPreparedStatementInterceptor implements InstanceMethodsAroundInterceptor {
    @Override
    public void beforeMethod( Object object, Method method, Object[] allArguments, Class<?>[] parameterTypes) {
        log.info("before mysql method name:{},args:{}",method.getName(), Arrays.toString(allArguments));

        ContextManager.startEntry(object.toString(), Comment.MYSQL);
    }

    @Override
    public Object afterMethod(  Object object,Method method, Object[] allArguments, Class<?>[] parameterTypes, Object ret) {
        ContextManager.exitEntry();
        log.info("mysql result:{}",ret);
        return ret;
    }

    @Override
    public void handleEx(   Object object,Method method, Object[] allArguments, Class<?>[] parameterTypes, Throwable t) {
        AbstractSpan abstractSpan = ContextManager.activeSpan();
        abstractSpan.setThrowableLog(t);
        log.error("mysql error",t);
    }
}
